package com.yoke.controller.tongji;

import com.yoke.common.JsonMessage;
import com.yoke.common.base.NginxBaseControl;
import com.yoke.common.tools.UrlPath;
import com.yoke.controller.copy.CopyIndexControl;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * Created by jiangzeyin on 2017/8/9.
 */
@Controller
@RequestMapping("tongji")
public class TongJiIndexControl extends NginxBaseControl {


    @RequestMapping(value = "index.html", produces = MediaType.TEXT_HTML_VALUE)
    public String index(String name) throws IOException {
        request.setAttribute("data", CopyIndexControl.getInfo(UrlPath.Type.None, name, getConfPath()));
        return "tongji/index";
    }

    @RequestMapping(value = "details.html", produces = MediaType.TEXT_HTML_VALUE)
    public String details(String url) throws IOException {
        return "tongji/details";
    }

    /**
     * 获取数据详情
     *
     * @param url
     * @param dateType
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "data.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String data(String url, String dateType, String startTime, String endTime, String dime) throws IOException {
        if ("day".equals(dime)) {
            TimesUtil timesUtil = new TimesUtil(TimesUtil.Type.Click);
            return timesUtil.run(url, startTime, endTime, dateType);
        }
        if ("hours".equals(dime)) {
            TimesUtil timesUtil = new TimesUtil(TimesUtil.Type.ClickHours);
            return timesUtil.run(url, startTime, endTime, dateType);
        }
        return JsonMessage.getString(100, "时间维度不正确");
    }

    /**
     * 状态码
     *
     * @param url
     * @param dateType
     * @param startTime
     * @param endTime
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "data_status.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String data(String url, String dateType, String startTime, String endTime) throws IOException {
        TimesUtil timesUtil = new TimesUtil(TimesUtil.Type.Status);
        return timesUtil.run(url, startTime, endTime, dateType);
    }
}
